package com.pxmeta.dynamic.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pxmeta.dynamic.entity.Dynamic;
import com.pxmeta.dynamic.entity.TopicDynamic;
import com.pxmeta.dynamic.mapper.TopicDynamicMapper;
import com.pxmeta.dynamic.service.ITopicDynamicService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wcl
 * @since 2022-07-13
 */
@Service
public class TopicDynamicServiceImpl extends ServiceImpl<TopicDynamicMapper, TopicDynamic> implements ITopicDynamicService {

    /**
     * 获取话题下动态数量
     * @param topicId
     * @return
     */
    @Override
    public Long getDynamicCount(Long topicId) {
        LambdaQueryWrapper<TopicDynamic> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(TopicDynamic::getTopicId, topicId);
        return this.count(wrapper);
    }

    @Override
    public List<Long> getDynamicIds(Long topicId,Integer page, Integer size) {
        LambdaQueryWrapper<TopicDynamic> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(TopicDynamic::getTopicId,topicId);
        return this.page(new Page<>(page,size)).getRecords().stream().map(TopicDynamic::getDynamicId).collect(Collectors.toList());
    }

    @Override
    public void deleteByDynamicId(Long dynamicId) {
        LambdaQueryWrapper<TopicDynamic> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(TopicDynamic::getDynamicId, dynamicId);
        this.remove(wrapper);
    }
}
